package com.material.vtool.controller;

import com.google.common.collect.Maps;
import com.material.vtool.entity.BUserMaterialTypeEntity;
import com.material.vtool.entity.combinedEntity.HomePageMaterialTypeEntity;
import com.material.vtool.entity.combinedEntity.SimplyMaterialTypeEntity;
import com.material.vtool.entity.respEntity.RespData;
import com.material.vtool.entity.respEntity.ReturnCode;
import com.material.vtool.service.UserMaterialTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.HashMap;
import java.util.List;

/**
 * 用户频道
 */
@RestController
public class UserMaterialTypeController {
    @Autowired
    private UserMaterialTypeService userMaterialTypeService;

    /**
     * 首页：频道列表
     * @param userId 用户ID
     * @param miniCode 小程序标识
     * @return
     */
    @GetMapping(value = "/homePageMaterialTypeList", produces = "application/json;charset=UTF-8")
    public RespData homePageMaterialTypeList(@RequestHeader("userId") String userId, @RequestHeader("miniCode") String miniCode) {
        List<HomePageMaterialTypeEntity> list = userMaterialTypeService.getHomePageMaterialTypeList(Integer.valueOf(userId), miniCode);

        HashMap<String, Object> resultMap = Maps.newHashMapWithExpectedSize(10);
        resultMap.put("materialTypeList", list);

        RespData respData = new RespData();
        respData.setCode(ReturnCode.NORMAL_SUCC);
        respData.setMsg("成功");
        respData.setData(resultMap);
        return respData;
    }

    /**
     * 订阅频道：我的频道
     * @param userId 用户ID
     * @param miniCode 小程序标识
     * @return
     */
    @GetMapping(value = "/myMaterialTypeInfo", produces = "application/json;charset=UTF-8")
    public RespData myMaterialTypeInfo(@RequestHeader("userId") String userId, @RequestHeader("miniCode") String miniCode) {
        List<SimplyMaterialTypeEntity> list = userMaterialTypeService.getUserMaterialType(Integer.valueOf(userId), miniCode);

        HashMap<String, Object> resultMap = Maps.newHashMapWithExpectedSize(10);
        resultMap.put("materialTypeInfo", list);

        RespData respData = new RespData();
        respData.setCode(ReturnCode.NORMAL_SUCC);
        respData.setMsg("成功");
        respData.setData(resultMap);
        return respData;
    }

    /**
     * 订阅频道：热门频道
     * @param userId 用户ID
     * @param miniCode 小程序标识
     * @return
     */
    @GetMapping(value = "/hotMaterialTypeInfo", produces = "application/json;charset=UTF-8")
    public RespData hotMaterialTypeInfo(@RequestHeader("userId") String userId, @RequestHeader("miniCode") String miniCode) {
        List<SimplyMaterialTypeEntity> list = userMaterialTypeService.getHotMaterialType(Integer.valueOf(userId), miniCode);

        HashMap<String, Object> resultMap = Maps.newHashMapWithExpectedSize(10);
        resultMap.put("materialTypeInfo", list);

        RespData respData = new RespData();
        respData.setCode(ReturnCode.NORMAL_SUCC);
        respData.setMsg("成功");
        respData.setData(resultMap);
        return respData;
    }

    /**
     * 添加频道
     * @param userId
     * @param simplyMaterialTypeEntity
     * @return
     */
    @PostMapping(value = "/addUserMaterialType", produces = "application/json;charset=UTF-8")
    public RespData addUserMaterialType(@RequestHeader("userId") String userId, @RequestHeader("miniCode") String miniCode, @RequestBody SimplyMaterialTypeEntity simplyMaterialTypeEntity) {
        if (simplyMaterialTypeEntity == null || simplyMaterialTypeEntity.getId() == null) {
            RespData respData = new RespData();
            respData.setCode(ReturnCode.CLIENT_ERROR);
            respData.setMsg("参数错误");
            return respData;
        }
        Integer currentUserId = Integer.valueOf(userId);
        // 获取最小排序数
        int minSort = userMaterialTypeService.getUserMaterialTypeMinSort(currentUserId, miniCode);
        /*// 查看是否存在该记录
        BUserMaterialTypeEntity bUserMaterialTypeEntity = userMaterialTypeService.getUserMaterialTypeByMaterialTypeId(currentUserId, simplyMaterialTypeEntity.getId());
        // 如果存在，则更新状态；如果不存在，则新增
        if (bUserMaterialTypeEntity != null) {
            BUserMaterialTypeEntity entity = new BUserMaterialTypeEntity();
            entity.setId(bUserMaterialTypeEntity.getId());
            entity.setStatus(1);
            entity.setSort(minSort--);
            entity.setModifyTime(new Date());
            userMaterialTypeService.updateUserMaterialType(entity);
        } else {
            BUserMaterialTypeEntity entity = new BUserMaterialTypeEntity();
            entity.setUserId(currentUserId);
            entity.setMaterialTypeId(simplyMaterialTypeEntity.getId());
            entity.setStatus(1);
            entity.setSort(minSort--);
            entity.setCreateTime(new Date());
            userMaterialTypeService.addUserMaterialType(entity);
        }*/

        BUserMaterialTypeEntity entity = new BUserMaterialTypeEntity();
        entity.setUserId(currentUserId);
        entity.setMaterialTypeId(simplyMaterialTypeEntity.getId());
        entity.setStatus(1);
        entity.setSort(minSort == 0 ? 0 : minSort--);
        entity.setCreateTime(new Date());
        userMaterialTypeService.addUserMaterialType(entity);

        RespData respData = new RespData();
        respData.setCode(ReturnCode.NORMAL_SUCC);
        respData.setMsg("成功");
        return respData;
    }

    /**
     * 删除频道
     * @param simplyMaterialTypeEntity
     * @return
     */
    @PostMapping(value = "/deleteUserMaterialType", produces = "application/json;charset=UTF-8")
    public RespData deleteUserMaterialType(@RequestBody SimplyMaterialTypeEntity simplyMaterialTypeEntity) {
        if (simplyMaterialTypeEntity == null || simplyMaterialTypeEntity.getId() == null) {
            RespData respData = new RespData();
            respData.setCode(ReturnCode.CLIENT_ERROR);
            respData.setMsg("参数错误");
            return respData;
        }

        // 根据主键 ID 删除用户的频道
        userMaterialTypeService.deleteUserMaterialType(Integer.valueOf(simplyMaterialTypeEntity.getId()));

        RespData respData = new RespData();
        respData.setCode(ReturnCode.NORMAL_SUCC);
        respData.setMsg("成功");
        return respData;
    }

    /**
     * 编辑频道，拖拽排序
     * @param userId 用户ID
     * @param list 要交换数据的两个列表
     * @return
     */
    @PostMapping("/resortUserMaterialType")
    @Transactional
    public RespData resortUserMaterialType(@RequestHeader("userId") String userId, @RequestBody List<SimplyMaterialTypeEntity> list) {
        if (list == null || list.size() != 2) {
            RespData respData = new RespData();
            respData.setCode(ReturnCode.CLIENT_ERROR);
            respData.setMsg("参数错误");
            return respData;
        }

        SimplyMaterialTypeEntity first = list.get(0);
        SimplyMaterialTypeEntity second = list.get(1);

        BUserMaterialTypeEntity bUserMaterialTypeEntity = new BUserMaterialTypeEntity();
        bUserMaterialTypeEntity.setUserId(Integer.valueOf(userId));
        bUserMaterialTypeEntity.setMaterialTypeId(first.getId());
        bUserMaterialTypeEntity.setSort(second.getSort());
        bUserMaterialTypeEntity.setModifyTime(new Date());

        BUserMaterialTypeEntity bUserMaterialTypeEntity1 = new BUserMaterialTypeEntity();
        bUserMaterialTypeEntity1.setUserId(Integer.valueOf(userId));
        bUserMaterialTypeEntity1.setMaterialTypeId(second.getId());
        bUserMaterialTypeEntity1.setSort(first.getSort());
        bUserMaterialTypeEntity1.setModifyTime(new Date());

        userMaterialTypeService.resortUserMaterialType(bUserMaterialTypeEntity);
        userMaterialTypeService.resortUserMaterialType(bUserMaterialTypeEntity1);

        RespData respData = new RespData();
        respData.setCode(ReturnCode.NORMAL_SUCC);
        respData.setMsg("成功");
        return respData;
    }

    /**
     * 订阅频道：保存我的频道
     * @param userId 用户ID
     * @param list 频道列表
     * @return
     */
    /*@PostMapping(value = "/saveUserMaterialType", produces = "application/json;charset=UTF-8")
    public RespData saveUserMaterialType(@RequestHeader("userId") String userId, @RequestBody List<SimplyMaterialTypeEntity> list) {
        if (list == null || list.size() <= 0) {
            RespData respData = new RespData();
            respData.setCode(ReturnCode.CLIENT_ERROR);
            respData.setMsg("参数异常");
            return respData;
        }

        userMaterialTypeService.saveUserMaterialType(Integer.valueOf(userId), list);

        RespData respData = new RespData();
        respData.setCode(ReturnCode.NORMAL_SUCC);
        respData.setMsg("成功");
        return respData;
    }*/

    /**
     * 订阅频道：保存我的频道
     * @param userId 用户ID
     * @param materialTypeIds 频道列表
     * @return
     */
    @PostMapping(value = "/saveUserMaterialType", produces = "application/json;charset=UTF-8")
    public RespData saveUserMaterialType(@RequestHeader("userId") String userId, @RequestParam Integer[] materialTypeIds) {
        userMaterialTypeService.saveUserMaterialTypeArray(Integer.valueOf(userId), materialTypeIds);

        RespData respData = new RespData();
        respData.setCode(ReturnCode.NORMAL_SUCC);
        respData.setMsg("成功");
        return respData;
    }
}
